//智能生成购物清单
const { Recipe } = require('../../database');

exports.generateShoppingList = async (req, res) => {
  try {
    const { recipeIds } = req.body;

    const recipes = await Recipe.findAll({
      where: {
        id: recipeIds
      }
    });

    const shoppingList = {};
    // 遍历食谱，将食材和数量添加到购物清单中
    recipes.forEach((recipe) => {
      // 假设每个食谱的 ingredients 字段包含一个数组，每个元素是一个包含 name 和 quantity 的对象
      recipe.ingredients.forEach((ingredient) => {
        if (shoppingList[ingredient.name]) {

          shoppingList[ingredient.name] += ingredient.quantity;
        } else {
          shoppingList[ingredient.name] = ingredient.quantity;
        }
      });
    });

    res.json(shoppingList);
  } catch (error) {
    console.error('生成购物清单出错:', error);
    res.status(500).json({ error: '生成购物清单失败' });
  }
};
